package com.example.hearthstoneagain;

import java.util.ArrayList;
import java.util.HashMap;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Build;

public class MainNewsActivity extends ActionBarActivity {

	private Context context;
	private ArrayList<HashMap<String, String>> listanews;
	private ListView lst;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_news);
		
		context = this.getApplicationContext();
		
		BancodeDados banco = new BancodeDados(context);
		
		SQLiteDatabase dtb = banco.getReadableDatabase();
		
		String nomecol[] = {"titulo","img"};
		
		Cursor resp_news = dtb.query("news", nomecol, null, null, null, null, null);
		
		resp_news.moveToFirst();
		
		listanews = new ArrayList<HashMap<String,String>>();
		
		for(int i=0;i<resp_news.getCount();i++) {
			HashMap<String, String> hm = new HashMap<String,String>();
			hm.put("ttl", resp_news.getString(0));
			//String substr = resp_news.getString(1).substring(0, 40)+"...";
			
			//hm.put("sub", substr);
			hm.put("back",Integer.toString(resp_news.getInt(1)));
			resp_news.moveToNext();
			listanews.add(hm);
		}
		
		lst = (ListView) this.findViewById(R.id.listView1);
		
		String[] from = { "ttl"};

		int[] to = { R.id.ttl};
		
		dtb.close();
		
		CustomAdapter2 adapter = new CustomAdapter2(getBaseContext(), listanews, R.layout.listview_layout3, from, to);
		
		lst.setAdapter(adapter);
		
		lst.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
				HashMap<String, String> hm = (HashMap<String, String>) lst.getAdapter().getItem(arg2);
				
				String news = hm.get("ttl");
				
				Intent intent = new Intent (MainNewsActivity.this,NewsActivity.class);
				intent.putExtra("titulo", news);
				
				TabGroupActivity parentActivity = (TabGroupActivity)getParent();
				parentActivity.startChildActivity("NewsActivity", intent);
				
				//startActivity(intent);
			}
		});
	}

}
